var a = getApp(), t = require("../../utils/api.js");

Page({
    data: {
        info: "",
        page: "",
        curStatus: 0,
        is_use: "",
        is_get: "",
        footer: [],
        carnum: wx.getStorageSync("cartnum"),
        showLoding: !0
    },
    onLoad: function(e) {
        var o = this, n = a.globalData.openid;
        o.setData({
            showLoding: !1
        }), t.getAllTicket({
            openid: n,
            type: 1
        }).then(function(a) {
            if (console.log(a.data), 0 == a.errno) {
                if (null == a.data.no_ues || 0 == a.data.no_ues.length) return console.log(1), o.setData({
                    showNo: !0,
                    showLoding: !0
                }), !1;
                var t = a.data.no_ues;
                o.autosize(t), o.getrange(t), o.setData({
                    info: t
                });
            } else o.setData({
                showNo: !0
            });
            o.setData({
                showLoding: !0
            });
        }), wx.setNavigationBarColor({
            frontColor: wx.getStorageSync("basic").topcolor,
            backgroundColor: wx.getStorageSync("basic").topbg
        });
    },
    autosize: function(a) {
        for (var t = this, e = [], o = 0; o < a.length; o++) {
            var n = a[o].cut_price;
            n.split("").length <= 5 && (e[o] = 66), 6 == n.split("").length && (e[o] = 56), 
            7 == n.split("").length && (e[o] = 46), n.split("").length > 7 && (e[o] = 36);
        }
        t.setData({
            autoSize: e
        });
    },
    getrange: function(a) {
        var t = this, e = [];
        console.log(a);
        for (var o = 0; o < a.length; o++) {
            var n = a[o].range;
            if (console.log(n), n.length <= 0 && (e[o] = "全场所有商品可用"), n.length > 0) {
                for (var s = "", r = 0; r < n.length; r++) r == n.length - 1 ? s += n[r].gc_name ? n[r].gc_name : n[r].g_name : s += n[r].gc_name ? n[r].gc_name + "、" : n[r].g_name + "、";
                e[o] = s;
            }
            console.log(e[o]);
        }
        t.setData({
            getranges: e
        }), console.log(t.data.getranges);
    },
    tel: function(e) {
        var o = this, n = e.currentTarget.dataset.id;
        if ("" == n || void 0 == n) return wx.showToast({
            title: "非法进入",
            icon: "none",
            duration: 2e3
        }), !1;
        t.userGetTicket({
            openid: a.globalData.openid,
            tid: n
        }).then(function(e) {
            console.log(e), 0 == e.errno ? (wx.showToast({
                title: "领取成功，快去使用吧",
                icon: "none",
                duration: 2e3
            }), t.getAllTicket({
                openid: a.globalData.openid
            }).then(function(a) {
                if (0 == a.errno) {
                    var t = a.data.normal;
                    o.autosize(t), o.getrange(t), o.setData({
                        info: a.data.normal
                    });
                }
            })) : wx.showToast({
                title: e.message,
                icon: "none",
                duration: 2e3
            });
        });
    },
    couponTap: function(e) {
        var o = this, n = e.currentTarget.dataset.id;
        o.setData({
            showLoding: !1,
            showNo: !1
        }), t.getAllTicket({
            openid: a.globalData.openid,
            type: 1
        }).then(function(a) {
            if (0 == a.errno) {
                if (10 == n) t = a.data.use; else if (20 == n) t = a.data.over; else if (0 == n) var t = a.data.no_ues;
                if (null == t || 0 == t.length) return o.setData({
                    showNo: !0,
                    showLoding: !0
                }), !1;
                o.autosize(t), o.getrange(t), o.setData({
                    info: 0 == n ? a.data.no_ues : "",
                    is_over: 20 == n ? a.data.over : "",
                    is_use: 10 == n ? a.data.use : ""
                });
            } else o.setData({
                showNo: !0
            });
            o.setData({
                showLoding: !0
            });
        }), o.setData({
            curStatus: n
        });
    },
    getCurrentList: function(a) {
        this.setData({
            loading: !0,
            tip: ""
        });
    },
    sites: function(a) {
        var t = this;
        console.log(a);
        var e = a.currentTarget.dataset.data_index, o = t.data.footer;
        "url" == o.params.data[e].type ? wx.reLaunch({
            url: o.params.data[e].url
        }) : "tel" == o.params.data[e].type ? wx.makePhoneCall({
            phoneNumber: o.params.data[e].url
        }) : "out_link" == o.params.data[e].type ? wx.navigateTo({
            url: "/pages/template/out_h5?url=" + o.params.data[e].url
        }) : "app" == o.params.data[e].type && wx.navigateToMiniProgram({
            appId: o.params.data[e].url_name,
            path: o.params.data[e].url,
            envVersion: "develop",
            success: function(a) {}
        });
    },
    todetail: function(a) {
        console.log(a);
        var t = a.currentTarget.dataset.id, e = a.currentTarget.dataset.type;
        wx.navigateTo({
            url: "/pages/personal/couponDetail?id=" + t + "&type=" + e
        });
    },
    touse: function() {
        wx.navigateTo({
            url: "/pages/template/index"
        });
    }
});